home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / GNU_KIT / DISK8.ZIP / src / makest / commands.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-08  |  11.9 KB  |  488 lines

  1. /* Command processing for GNU Make.
  2. Copyright (C) 1988, 1989 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 1, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "dep.h"
  21. #include "commands.h"
  22. #include "file.h"
  23. #include "variable.h"
  24. #include "job.h"
  25.  
  26. extern int remote_kill ();
  27.  
  28. extern int getpid ();
  29.  
  30. /* Set FILE's automatic variables up.  */
  31.  
  32. static void
  33. set_file_variables (file)
  34.      register struct file *file;
  35. {
  36.   register char *p;
  37.   char *at, *percent, *star, *less;
  38.  
  39. #define    DEFINE_VARIABLE(name, len, value) \
  40.   (void) define_variable_for_file (name, len, value, o_automatic, 0, file)
  41.  
  42. #ifndef    NO_ARCHIVES
  43.   /* If the target is an archive member `lib(member)',
  44.      then $@ is `lib' and $% is `member'.  */
  45.  
  46.   if (ar_name (file->name))
  47.     {
  48.       p = index (file->name, '(');
  49.       at = savestring (file->name, p - file->name);
  50.       ++p;
  51.       percent = savestring (p, strlen (p) - 1);
  52.     }
  53.   else
  54. #endif    /* NO_ARCHIVES.  */
  55.     {
  56.       at = savestring (file->name, strlen (file->name));
  57.       percent = "";
  58.     }
  59.  
  60.   DEFINE_VARIABLE ("@", 1, at);
  61.   DEFINE_VARIABLE ("%", 1, percent);
  62.  
  63. #define    LASTSLASH(s)    rindex ((s), '/')
  64. #define    FILEONLY(s)    (p != 0 ? p + 1 : (s))
  65. #define    DIRONLY(s)    (p == 0 ? "./" : p == (s) ? "/" \
  66.              : savestring ((s), (p - (s)) + 1))
  67.  
  68.   /* $* is the stem from an implicit or static pattern rule.  */
  69.   if (file->stem == 0)
  70.     {
  71.       /* In Unix make, $* is set to the target name with
  72.      any suffix in the .SUFFIXES list stripped off for
  73.      explicit rules.  We store this in the `stem' member.  */
  74.       register struct dep *d;
  75.       for (d = enter_file (".SUFFIXES")->deps; d != 0; d = d->next)
  76.     {
  77.       unsigned int len = strlen (file->name);
  78.       unsigned int slen = strlen (dep_name (d));
  79.       if (len > slen && streq (dep_name (d), file->name + len - slen))
  80.         {
  81.           file->stem = savestring (file->name, len - slen);
  82.           break;
  83.         }
  84.     }
  85.       if (d == 0)
  86.     file->stem = "";
  87.     }
  88.   star = file->stem;
  89.  
  90.   DEFINE_VARIABLE ("*", 1, star);
  91.  
  92.   /* $< is the first dependency.  */
  93.   less = file->deps != 0 ? dep_name (file->deps) : "";
  94.  
  95.   if (file->cmds == default_file->cmds)
  96.     /* This file got its commands from .DEFAULT.
  97.        In this case $< is the same as $@.  */
  98.     less = at;
  99.  
  100.   DEFINE_VARIABLE ("<", 1, less);
  101.  
  102.   /* Set up the D and F versions.  */
  103.   p = LASTSLASH (at);
  104.   DEFINE_VARIABLE ("@D", 2, DIRONLY (at));
  105.   DEFINE_VARIABLE ("@F", 2, FILEONLY (at));
  106.   p = LASTSLASH (star);
  107.   DEFINE_VARIABLE ("*D", 2, DIRONLY (star));
  108.   DEFINE_VARIABLE ("*F", 2, FILEONLY (star));
  109.   p = LASTSLASH (less);
  110.   DEFINE_VARIABLE ("<D", 2, DIRONLY (less));
  111.   DEFINE_VARIABLE ("<F", 2, FILEONLY (less));
  112.   p = LASTSLASH (percent);
  113.   DEFINE_VARIABLE ("%D", 2, DIRONLY (percent));
  114.   DEFINE_VARIABLE ("%F", 2, FILEONLY (percent));
  115.  
  116.   /* Compute the values for $^ and $? and their F and D versions.  */
  117.  
  118.   {
  119.     register unsigned int caret_len, qmark_len;
  120.     char *caret_value, *caretD_value, *caretF_value;
  121.     register char *cp, *cDp, *cFp;
  122.     char *qmark_value, *qmarkD_value, *qmarkF_value;
  123.     register char *qp, *qDp, *qFp;
  124.     register struct dep *d;
  125.     unsigned int len;
  126.  
  127.     caret_len = qmark_len = 0;
  128.     for (d = file->deps; d != 0; d = d->next)
  129.       {
  130.     register unsigned int i = strlen (dep_name (d)) + 1;
  131.     caret_len += i;
  132.     if (d->changed)
  133.       qmark_len += i;
  134.       }
  135.  
  136.     len = caret_len == 0 ? 1 : caret_len;
  137.     cp = caret_value = (char *) xmalloc (len);
  138.     cDp = caretD_value = (char *) xmalloc (len);
  139.     cFp = caretF_value = (char *) xmalloc (len);
  140.     len = qmark_len == 0 ? 1 : qmark_len;
  141.     qp = qmark_value = (char *) xmalloc (len);
  142.     qDp = qmarkD_value = (char *) xmalloc (len);
  143.     qFp = qmarkF_value = (char *) xmalloc (len);
  144.  
  145.     for (d = file->deps; d != 0; d = d->next)
  146.       {
  147.     char *c, *cD, *cF;
  148.     unsigned int Dlen, Flen;
  149.  
  150.     c = dep_name (d);
  151.     len = strlen (c);
  152.     bcopy (c, cp, len);
  153.     cp += len;
  154.     *cp++ = ' ';
  155.  
  156.     p = LASTSLASH (c);
  157.     if (p == 0)
  158.       {
  159.         cF = c;
  160.         Flen = len;
  161.         cD = "./";
  162.         Dlen = 2;
  163.       }
  164.     else if (p == c)
  165.       {
  166.         cD = c;
  167.         Dlen = 1;
  168.         cF = c + 1;
  169.         Flen = len - 1;
  170.       }
  171.     else
  172.       {
  173.         cF = p + 1;
  174.         Flen = len - (p + 1 - c);
  175.         cD = c;
  176.         Dlen = p - c;
  177.       }
  178.     bcopy (cD, cDp, Dlen);
  179.     cDp += Dlen;
  180.     *cDp++ = ' ';
  181.     bcopy (cF, cFp, Flen);
  182.     cFp += Flen;
  183.     *cFp++ = ' ';
  184.  
  185.     if (d->changed)
  186.       {
  187.         bcopy (c, qp, len);
  188.         qp += len;
  189.         *qp++ = ' ';
  190.         bcopy (cD, qDp, Dlen);
  191.         qDp += Dlen;
  192.         *qDp++ = ' ';
  193.         bcopy (cF, qFp, Flen);
  194.         qFp += Flen;
  195.         *qFp++ = ' ';
  196.       }
  197.       }
  198.  
  199.     /* Kill the last spaces and define the variables.  */
  200.  
  201.     cp[cp > caret_value ? -1 : 0] = '\0';
  202.     DEFINE_VARIABLE ("^", 1, caret_value);
  203.     cDp[cDp > caretD_value ? -1 : 0] = '\0';
  204.     DEFINE_VARIABLE ("^D", 2, caretD_value);
  205.     cFp[cFp > caretF_value ? -1 : 0] = '\0';
  206.     DEFINE_VARIABLE ("^F", 2, caretF_value);
  207.  
  208.     qp[qp > qmark_value ? -1 : 0] = '\0';
  209.     DEFINE_VARIABLE ("?", 1, qmark_value);
  210.     qDp[qDp > qmarkD_value ? -1 : 0] = '\0';
  211.     DEFINE_VARIABLE ("?D", 2, qmarkD_value);
  212.     qFp[qFp > qmarkF_value ? -1 : 0] = '\0';
  213.     DEFINE_VARIABLE ("?F", 2, qmarkF_value);
  214.   }
  215.  
  216. #undef    LASTSLASH
  217. #undef    FILEONLY
  218. #undef    DIRONLY
  219.  
  220. #undef    DEFINE_VARIABLE
  221. }
  222.  
  223. /* Chop CMDS up into individual command lines if necessary.  */
  224.  
  225. void
  226. chop_commands (cmds)
  227.      register struct commands *cmds;
  228. {
  229.   if (cmds != 0 && cmds->command_lines == 0)
  230.     {
  231.       /* Chop CMDS->commands up into lines in CMDS->command_lines.
  232.      Also set the corresponding CMDS->lines_recurse elements,
  233.      and the CMDS->any_recurse flag.  */
  234.       register char *p;
  235.       unsigned int nlines, idx;
  236.       char **lines;
  237.  
  238.       nlines = 5;
  239.       lines = (char **) xmalloc (5 * sizeof (char *));
  240.       idx = 0;
  241.       p = cmds->commands;
  242.       while (*p != '\0')
  243.     {
  244.       char *end = p;
  245.     find_end:;
  246.       end = index (end, '\n');
  247.       if (end == 0)
  248.         end = p + strlen (p);
  249.       else if (end > p && end[-1] == '\\')
  250.         {
  251.           int backslash = 1;
  252.           register char *b;
  253.           for (b = end - 2; b >= p && *b == '\\'; --b)
  254.         backslash = !backslash;
  255.           if (backslash)
  256.         {
  257.           ++end;
  258.           goto find_end;
  259.         }
  260.         }
  261.  
  262.       if (idx == nlines)
  263.         {
  264.           nlines += 2;
  265.           lines = (char **) xrealloc ((char *) lines,
  266.                       nlines * sizeof (char *));
  267.         }
  268.       lines[idx++] = savestring (p, end - p);
  269.       p = end;
  270.       if (*p != '\0')
  271.         ++p;
  272.     }
  273.  
  274.       if (idx != nlines)
  275.     {
  276.       nlines = idx;
  277.       lines = (char **) xrealloc ((char *) lines,
  278.                       nlines * sizeof (char *));
  279.     }
  280.  
  281.       cmds->ncommand_lines = nlines;
  282.       cmds->command_lines = lines;
  283.  
  284.       cmds->any_recurse = 0;
  285.       cmds->lines_recurse = (char *) xmalloc (nlines);
  286.       for (idx = 0; idx < nlines; ++idx)
  287.     {
  288.       unsigned int len;
  289.       int recursive;
  290.       p = lines[idx];
  291.       len = strlen (p);
  292.       recursive = (sindex (p, len, "$(MAKE)", 7) != 0
  293.                || sindex (p, len, "${MAKE}", 7) != 0);
  294.       cmds->lines_recurse[idx] = recursive;
  295.       cmds->any_recurse |= recursive;
  296.     }
  297.     }
  298. }
  299.  
  300. /* Execute the commands to remake FILE.  If they are currently executing,
  301.    return or have already finished executing, just return.  Otherwise,
  302.    fork off a child process to run the first command line in the sequence.  */
  303.  
  304. void
  305. execute_file_commands (file)
  306.      struct file *file;
  307. {
  308.   register char *p;
  309.  
  310.   /* Don't go through all the preparations if
  311.      the commands are nothing but whitespace.  */
  312.  
  313.   for (p = file->cmds->commands; *p != '\0'; ++p)
  314.     if (*p != ' ' && *p != '\t' && *p != '\n' && *p != '-' && *p != '@')
  315.       break;
  316.   if (*p == '\0')
  317.     {
  318.       file->command_state = cs_finished;
  319.       file->update_status = 0;
  320.       notice_finished_file (file);
  321.       return;
  322.     }
  323.  
  324.   /* First set the automatic variables according to this file.  */
  325.  
  326.   initialize_file_variables (file);
  327.  
  328.   set_file_variables (file);
  329.  
  330.   /* Start the commands running.  */
  331.   new_job (file);
  332. }
  333.  
  334. #define    PROPAGATED_SIGNAL_MASK \
  335.   (sigmask (SIGTERM) | sigmask (SIGINT) | sigmask (SIGHUP) | sigmask (SIGQUIT))
  336.  
  337. /* Handle fatal signals.  */
  338.  
  339. int
  340. fatal_error_signal (sig)
  341.      int sig;
  342. {
  343.   signal (sig, SIG_DFL);
  344. #ifndef atarist
  345. #ifndef USG
  346.   (void) sigsetmask (0);
  347. #endif
  348.  
  349.   /* A termination signal won't be sent to the entire
  350.      process group, but it means we want to kill the children.  */
  351.  
  352.   if (sig == SIGTERM)
  353.     {
  354.       register struct child *c;
  355.       push_signals_blocked_p (1);
  356.       for (c = children; c != 0; c = c->next)
  357.     if (!c->remote)
  358.       (void) kill (c->pid, SIGTERM);
  359.       pop_signals_blocked_p ();
  360.     }
  361.  
  362.   /* If we got a signal that means the user
  363.      wanted to kill make, remove pending targets.  */
  364.  
  365.   if (PROPAGATED_SIGNAL_MASK & sigmask (sig))
  366.     {
  367.       register struct child *c;
  368.       push_signals_blocked_p (1);
  369.  
  370.       /* Remote children won't automatically get signals sent
  371.      to the process group, so we must send them.  */
  372.       for (c = children; c != 0; c = c->next)
  373.     if (c->remote)
  374.       (void) remote_kill (c->pid, sig);
  375.  
  376.       for (c = children; c != 0; c = c->next)
  377.     delete_child_targets (c);
  378.  
  379.       pop_signals_blocked_p ();
  380.  
  381.       /* Clean up the children.  We don't just use the call below because
  382.      we don't want to print the "Waiting for children" message.  */
  383.       wait_for_children (0, 0);
  384.     }
  385.   else
  386.     /* Wait for our children to die.  */
  387.     wait_for_children (0, 1);
  388.  
  389.   /* Delete any non-precious intermediate files that were made.  */
  390. #endif /* atarist */
  391.  
  392.   remove_intermediates (1);
  393.  
  394.   if (sig == SIGQUIT)
  395.     /* We don't want to send ourselves SIGQUIT, because it will
  396.        cause a core dump.  Just exit instead.  */
  397.     exit (1);
  398.  
  399.   /* Signal the same code; this time it will really be fatal.  */
  400.   if (kill (getpid (), sig) < 0)
  401.     /* It shouldn't return, but if it does, die anyway.  */
  402.     pfatal_with_name ("kill");
  403.  
  404.   return 0;
  405. }
  406.  
  407. /* Delete all non-precious targets of CHILD unless they were already deleted.
  408.    Set the flag in CHILD to say they've been deleted.  */
  409.  
  410. void
  411. delete_child_targets (child)
  412.      struct child *child;
  413. {
  414.   struct stat st;
  415.  
  416.   if (child->deleted)
  417.     return;
  418.  
  419.   /* Delete the file unless it's precious.  */
  420.   if (!child->file->precious
  421.       && stat (child->file->name, &st) == 0
  422.       && S_ISREG (st.st_mode)
  423.       && (time_t) st.st_mtime != child->file->last_mtime)
  424.     {
  425.       error ("*** Deleting file `%s'", child->file->name);
  426.       if (unlink (child->file->name) < 0)
  427.     perror_with_name ("unlink: ", child->file->name);
  428.     }
  429.  
  430.   /* Also remove any non-precious targets listed
  431.      in the `also_make' member.  */
  432.   if (child->file->also_make != 0)
  433.     {
  434.       register unsigned int i;
  435.       for (i = 0; child->file->also_make[i] != 0; ++i)
  436.     {
  437.       char *name = child->file->also_make[i];
  438.       struct file *f = lookup_file (name);
  439.       if (f != 0 && f->precious)
  440.         continue;
  441.       if (stat (name, &st) == 0
  442.           && S_ISREG (st.st_mode)
  443.           && (f == 0 ||
  444.           (time_t) st.st_mtime == f->last_mtime))
  445.         {
  446.           error ("*** [%s] Deleting file `%s'", child->file->name, name);
  447.           if (unlink (name) < 0)
  448.         perror_with_name ("unlink: ", name);
  449.         }
  450.     }
  451.     }
  452.  
  453.   child->deleted = 1;
  454. }
  455.  
  456. /* Print out the commands in CMDS.  */
  457.  
  458. void
  459. print_commands (cmds)
  460.      register struct commands *cmds;
  461. {
  462.   register char *s;
  463.  
  464.   fputs ("#  commands to execute", stdout);
  465.  
  466.   if (cmds->filename == 0)
  467.     puts (" (built-in):");
  468.   else
  469.     printf (" (from `%s', line %u):\n", cmds->filename, cmds->lineno);
  470.  
  471.   s = cmds->commands;
  472.   while (*s != '\0')
  473.     {
  474.       char *end;
  475.  
  476.       while (*s == ' ' || *s == '\t' || *s == '\n')
  477.     ++s;
  478.  
  479.       end = index (s, '\n');
  480.       if (end == 0)
  481.     end = s + strlen (s);
  482.  
  483.       printf ("\t%.*s\n", end - s, s);
  484.  
  485.       s = end;
  486.     }
  487. }
  488.